#!/bin/bash

readonly DEFAULT_NICK='parabola-user' # ASSERT: as defined in ~/.purple/accounts.xml and ~/.purple/blist.xml
readonly NETWORK='irc.freenode.net'
readonly DEFAULT_ACCOUNT="${DEFAULT_NICK}@${NETWORK}"
readonly CURRENT_NICK=$(grep '<name>.*</name>' ~/.purple/accounts.xml | sed "s|.*<name>\(.*\)@${NETWORK}</name>.*|\1|")
readonly KEEP_NICK_MSG="Your Parabola IRC nick-name is currently '${CURRENT_NICK}'.\n\n\
Would you like to keep this nick-name, or choose another?"


# if   which gxmessage > /dev/null # FIXME: issue #2208
if   which zenity > /dev/null
then if [[ "${CURRENT_NICK}" == "${DEFAULT_NICK}" ]]  || \
        zenity --question --width=480                    \
               --title="Parabola IRC Login"              \
               --text="${KEEP_NICK_MSG}"                 \
               --ok-label="Choose a different nick-name" \
               --cancel-label="Keep this nick-name"
     then nick=$(zenity --entry --width=320          \
                        --title="Parabola IRC Login" \
                        --text="Choose a nick-name:" \
                        --entry-text=${CURRENT_NICK} )
          nick=$([[ "${nick// /}" ]] && echo ${nick// /} || echo ${DEFAULT_NICK})
     else nick=${CURRENT_NICK}
          sed -i 's|^Exec=.*|Exec=pidgin|' ~/Desktop/parabola-irc.desktop
          rm                               ~/parabola-irc.sh
     fi

     cd ~/.purple/
     sed "s|${DEFAULT_ACCOUNT}|${nick}@${NETWORK}|" ./accounts.default.xml > ./accounts.xml
     sed "s|${DEFAULT_ACCOUNT}|${nick}@${NETWORK}|" ./blist.default.xml    > ./blist.xml
fi

pidgin
